package com.example.kp.mci;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

public class UserParser {

	private static UserParser instance;
	public Context context = null;
	private PreferencesManager prefMan = PreferencesManager.getInstance();
	private ArrayList<User> userList = new ArrayList<User>();

	public static UserParser getInstance(Context context) {
		if (instance == null) {
			instance = new UserParser(context);
		}
		return instance;
	}

	private UserParser(Context context) {
		this.context = context;
		 loadAllUser();
	}

	public void loadAllUser() {
		JSONObject jsonObject = null;
		User newUser;
			try {
				StringBuilder builder = new StringBuilder();
				InputStream is = context.getAssets().open("User.json");
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(is));
				String line;
				while ((line = reader.readLine()) != null) {
					builder.append(line);
				}
				String json = builder.toString();				
//				jsonObject = new JSONObject(json);
				JSONArray jsonArray = new JSONArray(json);
				for(int i=0; i<jsonArray.length(); i++){
					JSONObject jUser = jsonArray.getJSONObject(i);
					newUser= new User(jUser.getString("name"));
					JSONObject prefs = jUser.getJSONObject("preferences");
					
					newUser.setFont(prefs.getJSONArray("http://registry.gpii.org/common/font").getJSONObject(0).getInt("values"));
					newUser.setFontSize(prefs.getJSONArray("http://registry.gpii.org/common/font_size").getJSONObject(0).getInt("values"));
					newUser.setLineSpacing(prefs.getJSONArray("http://registry.gpii.org/common/line_spacing").getJSONObject(0).getInt("values"));
					newUser.setColorTheme(prefs.getJSONArray("http://registry.gpii.org/common/color_theme").getJSONObject(0).getInt("values"));
					newUser.setVolume(prefs.getJSONArray("http://registry.gpii.org/common/speech_output_volume").getJSONObject(0).getInt("values"));
					newUser.setSpeed(prefs.getJSONArray("http://registry.gpii.org/common/speech_output_speed").getJSONObject(0).getInt("values"));
					newUser.setSpeechOutputOn(prefs.getJSONArray("http://registry.gpii.org/common/speech_output").getJSONObject(0).getBoolean("values"));
					newUser.setSubtitlesOn(prefs.getJSONArray("http://registry.gpii.org/common/speech_output_subtitle").getJSONObject(0).getBoolean("values"));
					newUser.setSpeechInputOn(prefs.getJSONArray("http://registry.gpii.org/common/speech_input").getJSONObject(0).getBoolean("values"));
					newUser.setFeedbackOn(prefs.getJSONArray("http://registry.gpii.org/common/speech_input_feedback").getJSONObject(0).getBoolean("values"));
					newUser.setSpeechFeedbackOn_subtitleFeedbackOff(prefs.getJSONArray("http://registry.gpii.org/common/speech_feedback_on_subtitle_feedback_off").getJSONObject(0).getBoolean("values"));
					newUser.setPoiListOn(prefs.getJSONArray("http://registry.gpii.org/common/poiListOn").getJSONObject(0).getBoolean("values"));
					userList.add(newUser);
				}
				

			} catch (IOException e) {
				Log.d("Fehler", "1 " + e);
			} catch (JSONException e) {
				Log.d("Fehler", "2 " + e);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		

	}

	public ArrayList<User> getUserList() {
		return userList;
	}

	public void saveUser() {

	}

}
